package com.juyu.miliao.scheduler.message;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.common.constant.FirstRechargeTypeEnum;
import com.juyu.miliao.common.constant.NewYearDayActivityRedisKey;
import com.juyu.miliao.common.constant.SecondRechargeTypeEnum;
import com.juyu.miliao.scheduler.domin.UserLuckDrawInfoDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @author xuanqicheng
 * @create 2018-26-18:28
 */
@Component
public class NewYearDayActivityMessage {

    private final Logger logger = LoggerFactory.getLogger("questionAndAnswerLog");
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 更新问答缓存
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue("NEW_YEAR_DAY_PAY_QUEUE"),
            exchange = @Exchange("NEW_YEAR_DAY_EXCHANGE"),
            key ="NEW_YEAR_DAY_PAY"))
    public void getUpdateQuestion(String message) {
        logger.info("MQ获取充值信息：{}",message);
        try {
            JSONObject json = JSONObject.parseObject(message);

            long userId = json.getLongValue("userId");

            int contentType = json.getIntValue("contentType");

            String luckDrawNumberKey = NewYearDayActivityRedisKey.NEW_YEAR_DAY_ACTIVITY_LUCK_DRAW_NUMBERS_BY_USER + userId;

            //获取用户活动充值信息对象
            String chargeKey = NewYearDayActivityRedisKey.NEW_YEAR_DAY_ACTIVITY_CHARGE_BY_USER_USER;
            String hashKey = Long.toString(userId);
            String temp = (String) stringRedisTemplate.opsForHash().get(chargeKey, hashKey);
            if(!StringUtils.isEmpty(temp)){
                UserLuckDrawInfoDTO obj = JSONObject.parseObject(temp, UserLuckDrawInfoDTO.class);
                //判断是否是首充
                if(obj.getFirstRechargeType()!= FirstRechargeTypeEnum.SUCCESS_FIRST_RECHARGE_AND_NO_LOTTERY.getCode()
                        && obj.getFirstRechargeType() !=FirstRechargeTypeEnum.SUCCESS_FIRST_RECHARGE_AND_SUCCESS_LOTTERY.getCode()){
                    obj.setFirstRechargeType(FirstRechargeTypeEnum.SUCCESS_FIRST_RECHARGE_AND_NO_LOTTERY.getCode());
                    obj.setFirstRechareTime(LocalDateTime.now());
                }else{
                    if(obj.getSecondRechargeType()!= SecondRechargeTypeEnum.SUCCESS_SECOND_RECHARGE_AND_NO_LOTTERY.getCode()
                            && obj.getSecondRechargeType()!=SecondRechargeTypeEnum.SUCCESS_SECOND_RECHARGE_AND_SUCCESS_LOTTERY.getCode()){
                        obj.setSecondRechargeType(SecondRechargeTypeEnum.SUCCESS_SECOND_RECHARGE_AND_NO_LOTTERY.getCode());
                        obj.setSecondRechargeTime(LocalDateTime.now());
                    }
                }
                //contentType 1 金币，2：vip
                logger.info("获取用户活动充值信息对象{}",obj);
                stringRedisTemplate.opsForHash().put(chargeKey, hashKey, JSONObject.toJSONString(obj));
                //增加抽奖次数
                if(contentType==1){
                    stringRedisTemplate.opsForValue().increment(luckDrawNumberKey, 1);
                }

            }else{
                UserLuckDrawInfoDTO userLuckDrawInfoDTO = new UserLuckDrawInfoDTO();
                userLuckDrawInfoDTO.setUserId(userId);
                userLuckDrawInfoDTO.setFirstRechargeType(FirstRechargeTypeEnum.SUCCESS_FIRST_RECHARGE_AND_NO_LOTTERY.getCode());
                userLuckDrawInfoDTO.setFirstRechareTime(LocalDateTime.now());
                userLuckDrawInfoDTO.setSecondRechargeType(0);

                stringRedisTemplate.opsForHash().put(chargeKey, hashKey, JSONObject.toJSONString(userLuckDrawInfoDTO));
                //增加抽奖次数,contentType 1 金币，2：vip
                if(contentType==1){
                    stringRedisTemplate.opsForValue().increment(luckDrawNumberKey, 1);
                }
                logger.info("获取用户活动充值信息对象{}",userLuckDrawInfoDTO);
            }
        } catch (Exception ex) {
            logger.info("获取用户活动异常{}",ex);
        }
    }

}
